package at.fhv.androideightpuzzle.network.message;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class StartPuzzleMessage implements IMessage{
	
	public static final int TYPE=4;
	
	private String m_puzzleId;
	private int[] m_parts;
	private int[] m_endState;
	private int m_emtpyPartNumber;
	
	public StartPuzzleMessage(String puzzleId,int[] parts,int[] endState,int emptyPartNumber){
		m_puzzleId=puzzleId;
		m_parts=parts;
		m_endState=endState;
		m_emtpyPartNumber=emptyPartNumber;
	}
	
	@SuppressWarnings("unchecked")
	public String getJson(){
		JSONObject jsonObj=new JSONObject();
		
		jsonObj.put("type",StartPuzzleMessage.TYPE);
		
		JSONObject payload=new JSONObject();
		
		JSONArray partsJsonArr=new JSONArray();
		
		for(int i=0;i<m_parts.length; i++){
			partsJsonArr.add(m_parts[i]);
		}
		
		JSONArray endStateJsonArr=new JSONArray();
		
		for(int i=0; i<m_endState.length; i++){
			endStateJsonArr.add(m_endState[i]);
		}
		
		payload.put("puzzleid",m_puzzleId);
		payload.put("parts",partsJsonArr);
		payload.put("endstate",endStateJsonArr);
		payload.put("emptypartnumber",m_emtpyPartNumber);
		
		jsonObj.put("payload",payload);
		
		return jsonObj.toJSONString();
	}
	
	public String getPuzzleId(){
		return m_puzzleId;
	}
	
	public int[] getParts(){
		return m_parts;
	}
	
	public int[] getEndState(){
		return m_endState;
	}
	
	public int getEmptyPartNumber(){
		return m_emtpyPartNumber;
	}
}
